package com.app.tablet.view;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.tablet.R;
import com.app.tablet.data.TabletData;

public class DosageListActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {

	private static final String TagProject = "Tablet-Project";
	private static final String TagPage = "DosageListActivity-Page ::";

	boolean[] isSelected;
	List<String> dosage;
	ListView listDosage;
	TextView titletxt;
	Button leftBtn;
	Button rightBtn;
	private int selectedPosition = -1;

	DosageAdapter dosageListAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_dosagelistpage);
		setHeaderContent();

		listDosage = (ListView) findViewById(R.id.list_dosage);
		listDosage.setOnItemClickListener(this);
		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);

		Resources res = getResources();
		dosage = Arrays.asList(res.getStringArray(R.array.tablet_dosage_items));
		isSelected = new boolean[dosage.size()];

		dosageListAdapter = new DosageAdapter(this, 0, dosage);
		listDosage.setAdapter(dosageListAdapter);

	}

	private void setHeaderContent() {
		leftBtn = (Button) findViewById(R.id.leftHeaderBtn);
		leftBtn.setText(R.string.cancelBtn);

		rightBtn = (Button) findViewById(R.id.rightHeaderBtn);
		rightBtn.setText(R.string.doneBtn);

		titletxt = (TextView) findViewById(R.id.titleHeaderTxt);
		titletxt.setText(R.string.titleDosagelistpageageTxt);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftHeaderBtn:
			this.finish();
			break;
		case R.id.rightHeaderBtn:
			if (selectedPosition != -1) {
				AddPageActivity.dbData.setDoasge(dosage.get(selectedPosition));
				this.finish();
			}else{
				Toast.makeText(getApplicationContext(), "Choose a dosage", 500).show();
			}
			break;
		}
	}

	class DosageAdapter extends ArrayAdapter<String> {

		Context adcontext;

		public DosageAdapter(Context context, int textViewResourceId,
				List<String> objects) {
			super(context, textViewResourceId, objects);
			adcontext = context;
		}

		@Override
		public int getCount() {
			return dosage.size();

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) adcontext.getSystemService(adcontext.LAYOUT_INFLATER_SERVICE);
			View rowview = inflater.inflate(R.layout.layout_listitem_dosagelist, null);
			TextView txtname = (TextView) rowview.findViewById(R.id.txt_dosae);
			ImageView imgvw = (ImageView) rowview.findViewById(R.id.img_tick);
			txtname.setText(dosage.get(position));
			if (isSelected[position]) {
				imgvw.setVisibility(View.VISIBLE);
			} else {
				imgvw.setVisibility(View.GONE);
			}
			return rowview;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		for (int index = 0; index < isSelected.length; index++) {
			isSelected[index] = false;
		}
		isSelected[position] = true;
		selectedPosition = position;
		refreshListView();
	}

	private void refreshListView() {
		listDosage.invalidate();
		dosageListAdapter.notifyDataSetChanged();
	}
}
